//
//  PlayerModel.swift
//  dataBaseProj
//
//  Created by sea on 2020/1/7.
//  Copyright © 2020 CDSCRB. All rights reserved.
//

import UIKit
@objcMembers
class PlayerModel: NSObject {
    /// 球员id
    var playerID:String?
    /// 号码
    var playerNum:String?
    /// 名字
    var playerName:String?
    /// 球队id
    var playerTeamID:String?

    /// 球员类型 替补 上场 教练
    var playerType:String? // 0 首发 1替补

    /// 比赛数据id
    var playerMatchDataID:String?
    
    var matchInfo:MatchDataModel?{
        get{
            if !DYStringIsEmpty(value: self.playerMatchDataID as AnyObject?) {
                let model:MatchDataModel = MatchDataModel.getMatchDataModelWithID(matchDataID: self.playerMatchDataID!)
                return model
            }else{
                return MatchDataModel()
            }
        }
    }
    
    /// 取得球员信息
    /// - Parameters:
    ///   - playerId: 球员id
    ///   - CompeleteBlock: 完成
    class func getPlayerModelWithPlayerID(playerId:String, CompeleteBlock:@escaping compeletBlock){
        let sqlString = "select * from \(playerTableName) where \(DHPlayerID) = '\(playerId)'"
        DHDataBaseManager.shareDataBaseManager.searchWithSql(sql: sqlString) { (result) in
            if result is FMResultSet {
                let rs = result as! FMResultSet
                var next:Bool = false
                while rs.next() {
                    let playerModel = PlayerModel()
                    playerModel.playerID = rs.string(forColumn: DHPlayerID)
                    playerModel.playerNum = rs.string(forColumn: DHPlayerNum)
                    playerModel.playerName = rs.string(forColumn: DHPlayerName)
                    playerModel.playerTeamID = rs.string(forColumn: DHPlayerTeamID)
                    playerModel.playerType = rs.string(forColumn: DHPlayerType)
                    playerModel.playerMatchDataID = rs.string(forColumn: DHPlayerMatchDataID)
                    CompeleteBlock(playerModel)
                    next = true
                }
                rs.close()
                if !next {
                CompeleteBlock(NSNumber.init(value: false))
                }
            }else{
                CompeleteBlock(NSNumber.init(value: false))
            }
        }
    }
    
    class func getPlayerModelWithPlayerID(playerId:String) ->(PlayerModel){
        let sqlString = "select * from \(playerTableName) where \(DHPlayerID) = '\(playerId)'"
        var player:PlayerModel?
        DHDataBaseManager.shareDataBaseManager.searchWithSql(sql: sqlString) { (result) in
            if result is FMResultSet {
                let rs = result as! FMResultSet
                while rs.next() {
                    let playerModel = PlayerModel()
                    playerModel.playerID = rs.string(forColumn: DHPlayerID)
                    playerModel.playerNum = rs.string(forColumn: DHPlayerNum)
                    playerModel.playerName = rs.string(forColumn: DHPlayerName)
                    playerModel.playerTeamID = rs.string(forColumn: DHPlayerTeamID)
                    playerModel.playerType = rs.string(forColumn: DHPlayerType)
                    playerModel.playerMatchDataID = rs.string(forColumn: DHPlayerMatchDataID)
                    player = playerModel
                }
                rs.close()
            }
        }
        return player!
    }
}
